package com.example.springLearning.part7.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Service
public class ElectricService {

    /**
     * 不添加lazy注解会造成循环依赖的问题，解决办法有两种
     * 1.在添加的依赖上加上lazy注解
     * 2.使用SpringContentHolder获取已存在的bean
     */
    @Lazy
    @Autowired
    ElectricService electricService;

    @Autowired
    AdminUserService adminUserService;

    public void charge() throws Exception {
        System.out.println("Electric charging ...");
        electricService.pay();
    }

    public void pay() throws Exception {
        adminUserService.login();
        //String payNum = adminUserService.adminUser.getPayNum();
        String payNum = adminUserService.getAdminUser().getPayNum();
        System.out.println("User pay num: " + payNum);
        System.out.println("Pay with alipay ...");
        Thread.sleep(1000);
    }

}
